package com.iris.live.services.common;

import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 林平 on 2016/12/16.
 */
public class GPUtils {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(GPUtils.class);

    private static DecimalFormat df_2 = new DecimalFormat("0.00");

    public static Map<String, Object> getGPInfo(Map<ParameterName, Double> data) {
        Map<String, Object> gpValues = new HashMap<>();

        try {
            gpValues.put("GP1", "");
            gpValues.put("GP1%", "");
            gpValues.put("GP1dot5", "");
            gpValues.put("GP1dot5%", "");
            gpValues.put("GP2", "");
            gpValues.put("GP2%", "");
            gpValues.put("GP3", "");
            gpValues.put("GP3%", "");
            gpValues.put("Sales T.GP", "");
            gpValues.put("Sales T.GP%", "");

            if (data.get(ParameterName.车辆销售价) == null || "".equals(data.get(ParameterName.车辆销售价)) ||
                    "0".equals(data.get(ParameterName.车辆销售价)) || "0.00".equals(data.get(ParameterName.车辆销售价))) {
                return gpValues;
            }

            double 车辆销售价 = toDouble(data, ParameterName.车辆销售价); //含税
            double 消费税 = toDouble(data, ParameterName.消费税);
            double 采购成本 = toDouble(data, ParameterName.采购价格);

            // GP1
            double gp1 = (车辆销售价 - 消费税 - 采购成本) / 1.17;
            gpValues.put("GP1", df_2.format(gp1));
            gpValues.put("GP1%", df_2.format(100 * gp1 / ((车辆销售价 - 消费税) / 1.17)));
            // GP2 行政界面不显示
            double gp2 = gp1 - toDouble(data, ParameterName.经销商随车成本);
            gpValues.put("GP2", df_2.format(gp2));
            gpValues.put("GP2%", df_2.format(100 * gp2 / ((车辆销售价 - 消费税) / 1.17)));
//            //GP2
//            double gp2 = gp1dot5 - toDouble(data, ParameterName.厂商非购置税随车成本) - toDouble(data, ParameterName.贴息金额);
//            gpValues.put("GP2", df_2.format(gp2));
//            gpValues.put("GP2%", df_2.format(100 * gp2 / ((车辆销售价 - 消费税) / 1.17)));
            //GP3 行政界面不显示
            double gp3 = gp2 + toDouble(data, ParameterName.厂家批售SI)
                    + toDouble(data, ParameterName.厂家零售SI)/* + toDouble(data, ParameterName.厂家SI_购置税成本)*/;
            gpValues.put("GP3", df_2.format(gp3));
            gpValues.put("GP3%", df_2.format(100 * gp3 / ((车辆销售价 - 消费税) / 1.17)));
            // GP4 同GP3
            double gp4 = gp3;
            gpValues.put("GP4", df_2.format(gp4));
            gpValues.put("GP4%", df_2.format(100 * gp4 / ((车辆销售价 - 消费税) / 1.17)));
            //SalesTGP
            double freebieGp = toDouble(data, ParameterName.精品毛利) / 1.17;
            double financeCommissionFee = toDouble(data, ParameterName.金融手续费) / 1.06 + toDouble(data, ParameterName.金融手续调整成本差值) / 1.06;
            double financeServiceFee = toDouble(data, ParameterName.金融服务费) / 1.17;
            double insuranceCommissionFee = toDouble(data, ParameterName.交强险手续费) / 1.06
                    + toDouble(data, ParameterName.商业险手续费) / 1.06
//                    + toDouble(data, ParameterName.盗抢险手续费) / 1.06
                    + toDouble(data, ParameterName.保险手续费调整成本差值) / 1.06;
            double exInsuranceGp = toDouble(data, ParameterName.延保毛利);
            double secondFee = toDouble(data, ParameterName.置换手续费) / 1.06;
            double licenseFee = toDouble(data, ParameterName.上牌服务费毛利);
            double otherOneGp = toDouble(data, ParameterName.其他一毛利);
            double otherTwoGp = toDouble(data, ParameterName.其他二毛利);
            double salesGp = toDouble(data, ParameterName.销售服务费) / 1.17;
            double otherThrGp = toDouble(data, ParameterName.其他三毛利);
            double otherForGp = toDouble(data, ParameterName.其他四毛利);
            double otherFivGp = toDouble(data, ParameterName.其他五毛利);
            double freebiePrice = toDouble(data, ParameterName.后市场销售价);
            double exInsuranceGroup = toDouble(data, ParameterName.厂家延保实收金额);
            double exInsuranceThird = toDouble(data, ParameterName.第三方延保实收金额);
            double tgp = gp4 + freebieGp + financeCommissionFee + financeServiceFee + insuranceCommissionFee
                    + exInsuranceGp + secondFee + licenseFee + otherOneGp + otherTwoGp + salesGp
                    + otherThrGp + otherForGp + otherFivGp;
            gpValues.put("GP4.5", df_2.format(tgp));
            gpValues.put("GP4.5%", df_2.format(100 * tgp / ((车辆销售价 - 消费税 + freebiePrice + exInsuranceGroup
                    + exInsuranceThird) / 1.17 + financeCommissionFee + financeServiceFee + insuranceCommissionFee
                    + licenseFee + otherOneGp + otherTwoGp + salesGp + otherThrGp + otherForGp + otherFivGp)));
        } catch (Exception exc) {
            logger.error("GP 计算异常", exc);
        }

        return gpValues;
    }

    private static double toDouble(Map<ParameterName, Double> data, ParameterName column) {
        return toDouble(data, column, 0);
    }

    private static double toDouble(Map<ParameterName, Double> data, ParameterName column, double defaultValue) {
        if (!data.containsKey(column)) {
            logger.warn("No column found for " + column.name());

            return defaultValue;
        }

        try {
            Double value = data.get(column);
            if (value == null || Double.isNaN(value) || Double.isInfinite(value)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("invalid value is for column = " + column + ", value=" + value);
                }

                return defaultValue;
            }

            return value;
        }catch(Exception exc){
            logger.error("参数名=" + column.name(), exc);

            return defaultValue;
        }
    }

    public enum ParameterName {
        车辆销售价, 消费税, 采购价格, 贴息金额, 厂家SI_购置税成本, 经销商随车成本, 精品赠送成本, 延保赠送成本, 厂商非购置税随车成本,
        厂家批售SI, 厂家零售SI, 精品毛利, 延保毛利, 金融手续费, 金融服务费, 后市场销售价, 后市场成本, 五星延保销售价,
        /*五星延保成本*//*现金销售五星延保毛利,*/ 交强险手续费, 商业险手续费, 盗抢险手续费, 置换手续费, 其他服务费, 上牌服务费毛利,
        其他一毛利, 其他二毛利, 销售服务费, 其他三毛利, 其他四毛利, 其他五毛利, 厂家延保实收金额, 第三方延保实收金额,
        精品调整成本差值, 金融手续调整成本差值, 保险手续费调整成本差值,上牌服务费 /*延保调整成本差值*/
    }
}
